// pages/deposit/deposit.js
var app = getApp()
var api = require('../../config/api.js');
var comm = require("../../utils/comm.js")
Page({

  /**
   * 页面的初始数据
   */
  data: {
    addstatus: true,
    orderPrice: 0,
    applyNum: 0,

    myAgentList: {
      hasMore: true,
      list: [],
      listSize: 0
    },

    startNum: 1, //页数
  },




  //上拉获取列表
  hotActivityLoadMore: function(e) {
    var that = this;
    that.listPage({});

  },

  //分页查询列表
  listPage: function(data = {}) {
    var _that = this;
    //判断是否还有下一页
    if (_that.data.myAgentList.hasMore == true) {
      wx.showLoading({
        title: '加载中',
      })
      var pageNum = _that.data.startNum
      var _url = '?current=' + pageNum + '&size=5';

      comm.request(api.PullLoadMorePanter + _url, data, "POST").then(function(res) {
        wx.hideLoading();
        if (res.code == '1') {
          var listSize = res.obj.records.length;
          console.log('listSize=' + listSize);
          var newData = _that.data.myAgentList.list;
          var lis = res.obj.records;
          var total1 = res.obj.total;
          //concat是在原有基础上进行增加
          newData = newData.concat(lis);
          var newList = {};
          //当长度小于5表示没有下页数据了
          if (listSize < 5) {
            wx.showToast({
              title: "已加载全部列表",
              icon: 'none',
              duration: 2000
            });
            newList.hasMore = false;
          } else {
            newList.hasMore = true;
          }
          newList.list = newData;
          newList.listSize = listSize;
          for (var i = 0; i < listSize; i++) {
            if (newList.list[i].status == '1'){
              newList.list[i].status = '已支付'
            } else if (newList.list[i].status == '2'){
              newList.list[i].status = '已发货'
            } else if (newList.list[i].status == '3') {
              newList.list[i].status = '已完成'
            } else if (newList.list[i].status == '4') {
              newList.list[i].status = '商家取消'
            }
          }
          _that.setData({
            myAgentList: newList,
            total: total1
          });

          _that.setData({
            startNum: pageNum + 1 //页数加1
          })
        } else {
          // util.msg("错误", res.data.msg)
          console.log("网络通，数据不成功")
        }

      }).catch(function(res) {

        console.log("请求失败！")
      })
    } else {
      wx.showToast({
        title: "已加载全部商品",
        icon: 'none',
        duration: 2000
      });
    }
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    var that = this
    if (that.data.addstatus == true){
      that.listPage({})
    }
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {
    var that = this
    wx.request({
      url: api.SalerStu,
      data: {},
      method: 'GET',
      header: {
        'Accept': 'application/json',
        'ACCESS-TOKEN': wx.getStorageSync('token')
      },
      success: function(res) {
        if (res.data.success) {
          var orderPrice = res.data.obj.orderPrice;
          if (orderPrice >= 10000) {
            that.setData({
              addstatus: true,
              applyNum: res.data.obj.applyNum,
              orderPrice: res.data.obj.orderPrice,
            })
          }
          wx.hideLoading();
        }
      },
      fail: function() {
        wx.hideLoading();
        wx.showToast({
          title: "网络故障！",
          icon: 'none',
          duration: 3000
        });
      },
    })
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function() {

  }
})